let ctx = wx.createCanvasContext('myCanvas')
Page({
  data:{
    mosaic:true,
    imagePath:'',
  },
  open(){
    wx.chooseImage({
      count: 1,
    }).then(res=>{
      console.log(res)
      let file = res.tempFilePaths[0]
      ctx.drawImage(file,0,0,380,580)
      ctx.draw()
    })
  },
  clear(){
    this.setData({
      mosaic:false
    })
  },
  mosaic(){
    this.setData({
      mosaic:true
    })
  },
  move(e){
    console.log(e)
    if(this.data.mosaic){
      ctx.setFillStyle('#2CA8E8')
      ctx.fillRect(e.touches[0].x,e.touches[0].y,10,10)
      ctx.fillRect(e.touches[0].x+10,e.touches[0].y+10,10,10)
      ctx.setFillStyle('#EEFAFF')
      ctx.fillRect(e.touches[0].x+10,e.touches[0].y,10,10)
      ctx.fillRect(e.touches[0].x,e.touches[0].y,10,10)
      ctx.draw(true)
    }else{
      ctx.clearRect(e.touches[0].x,e.touches[0].y+10,20,20)
      ctx.clearRect(e.touches[0].x,e.touches[0].y,20,20)
      ctx.draw(true)
    }
      ctx.draw(true)
  },
  save(){
    console.log('保存')
    wx.canvasToTempFilePath({
       canvasId:'myCanvas'
    }).then(res=>{
        console.log('savefile:',res)
        this.setData({
          imagePath:res.tempFilePath
        })
        //调用此api 获取图片链接保存到系统相册
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.showToast({
              title: '保存图片成功！',
            })
          },
          fail(res) {
            wx.showToast({
              title: '保存图片失败！',
            })
          }
        })
      
    })
   // this.baocun();
  },
  seal(){
    ctx.setFillStyle('red')
    ctx.setFontSize(36)
    ctx.fillText('07200806程浩然',0,200)
    ctx.draw(true)
  }
})